package com.example.communitypro.service.impl;

import com.example.communitypro.mapper.UserActionsMapper;
import com.example.communitypro.mapper.UserDetailsMapper;
import com.example.communitypro.pojo.UserActions;
import com.example.communitypro.pojo.UserDetails;
import com.example.communitypro.pojo.UserInformation;
import com.example.communitypro.service.UserActionsService;
import com.example.communitypro.util.result.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DuplicateKeyException;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class UserActionsServiceImpl implements UserActionsService {
    @Autowired(required=false)
    private UserActionsMapper userActionsMapper;

    @Autowired(required=false)
    private UserDetailsMapper userDetailsMapper;

    @Override
    public Result queryUserFollow(int user_no, int user_action_type) {
        try {
            List<UserInformation> userInformations=userActionsMapper.getUserFollow(user_no,user_action_type);
            if(userInformations!=null)
            {
                List<UserDetails> userDetails=new ArrayList<>();
                for (UserInformation userInformation : userInformations){
                    userDetails.add(userDetailsMapper.selectByUser(userInformation.getUserNo()));
                }
                return Result.ok().message("查找关注或拉黑用户成功").data("userInformations",userInformations).data("userDetails",userDetails);
            }
            else{
                return Result.error().message("查找关注或拉黑用户失败");
            }
        }catch (Exception e){
            return Result.error().message("查找关注或拉黑用户失败");
        }
    }

    @Override
    public Result addUserActions(UserActions record){
        try {
            if(userActionsMapper.insertSelective(record)>0)
            {
                return Result.ok().message("新增用户操作成功");
            }
            else{
                return Result.error().message("新增用户操作失败");
            }
        }catch (Exception e){
            return Result.error().message("新增用户操作失败");
        }
    }


    @Override
    public Result dropUserActions(int UserActionsNo){
        try {
            if(userActionsMapper.deleteByPrimaryKey(UserActionsNo)>0)
            {
                return Result.ok().message("删除用户操作成功");
            }
            else{
                return Result.error().message("删除用户操作失败");
            }
        }catch (Exception e){
            return Result.error().message("删除用户操作失败");
        }
    }

    @Override
    public Result modifyUserActions(UserActions record){
        try {
            if(userActionsMapper.updateByPrimaryKeySelective(record)>0)
            {
                return Result.ok().message("修改成功");
            }
            else{
                return Result.error().message("修改失败：未找到对应记录");
            }
        }catch (DuplicateKeyException e){
            System.err.println(e);
            return Result.error().message("修改失败：已经存在对应记录");
        }
        catch (Exception e){
            System.err.println(e);
            return Result.error().message("修改失败："+e.toString());
        }
    }

    @Override
    public Result queryUserActions(int UserActionsNo){
        try {
            UserActions userActions=userActionsMapper.selectByPrimaryKey(UserActionsNo);
            if(userActions!=null)
            {
                return Result.ok().message("查找用户操作成功").data("userActions",userActions);
            }
            else{
                return Result.error().message("查找用户操作失败");
            }
        }catch (Exception e){
            return Result.error().message("查找用户操作失败");
        }
    }

    @Override
    public Result UserAction(UserActions record) {
        try {
            System.out.println(record);

            UserActions userActions = userActionsMapper.getUserAction(record.getUserNo(),record.getUserObjectNo(),record.getUserActionType());
            UserActions userActions1 = userActionsMapper.getUserAction2(record.getUserNo(),record.getUserObjectNo());
            System.out.println(userActions);
            if(userActions!=null)
            {
                userActionsMapper.deleteByPrimaryKey(userActions.getUserActionNo());
                if(userActions.getUserActionType()==0) {
                    return Result.ok().message("取消拉黑成功");
                }
                else {
                    return Result.ok().message("取消关注成功");
                }
            }
            else
            {
                if (userActions1!=null){
                    if (userActions1.getUserActionType()==0)
                    {
                        System.out.println("已经拉黑不能关注");
                        return Result.ok().message("已经拉黑不能关注");
                    }
                    else {
                        System.out.println("已经关注不能拉黑");
                        return Result.ok().message("已经关注不能拉黑");
                    }

                }
                else {
                    userActionsMapper.insertSelective(record);
                    if (record.getUserActionType() == 0) {
                        return Result.ok().message("拉黑成功");
                    } else {
                        return Result.ok().message("关注成功");
                    }
                }
            }
        }catch (Exception e){
            return Result.error().message("用户操作失败");
        }
    }

    @Override
    public Result judgeUserAction(UserActions record) {
        try {
            UserActions userActions=userActionsMapper.getUserAction(record.getUserNo(), record.getUserObjectNo(), record.getUserActionType());
            if(userActions!=null)
            {
                return Result.ok().message("用户已拉黑或关注").data("userActions",userActions);
            }
            else{
                return Result.error().message("查找用户操作失败");
            }
        }catch (Exception e){
            return Result.error().message("查找用户操作失败");
        }

    }
}
